// pages/register3/register3.js
import RegisterApi from '../../api/register'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    realName:'',
    userName:'',
    organizeId:'',
    phone:'',
    passWord:'',
    passWordNew:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      realName:options.realName,
      userName:options.userName,
      organizeId:options.organizeId,
      phone:options.phone
    })
  },
  /**
   * 返回
   */
  handlerGobackClick(){
    wx.navigateBack()
  },
  /**
   * 注册
   */
  async register() {
    const passWord = this.data.passWord
    const passWordNew = this.data.passWordNew
    if (passWord == '') {
      this._show_error('请输入密码')
      return
    }
    if (passWordNew == '') {
      this._show_error('请输入确认密码')
      return
    }
    if(passWord !== passWordNew){
      wx.showToast({
        title: '两次密码输入不一致',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(passWord.length < 6){
      wx.showToast({
        title: '密码不能小于6个字符',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(passWord.length > 12){
      wx.showToast({
        title: '密码不能大于12个字符',
        icon: 'none',
        duration: 2000
      })
      return
    }
    const param = {
      "loginName": this.data.userName,
      "organCode": this.data.organizeId,
      "password": this.data.passWord,
      "realName": this.data.realName,
      "telNum": this.data.phone
    }
    const res = await RegisterApi.userRegister(param)
    wx.showToast({
      title: '注册成功！',
      icon: 'success',
      duration: 2000
    })
    setTimeout(() => {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }, 2000)
  },
  /**
   * input 数据绑定
   */
  updateValue(e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail
    this.setData(nameMap)
  },
  _show_error(msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000
    })
  }
})
